<?php
/**
 * Created by PhpStorm.
 * User: li gui gong
 * Date: 2018/2/1
 * Time: 20:16
 */
namespace Admin\Model;
use Think\Model;
class GoodsModel extends Model
{
    //添加时调用create方法允许接收的字段(防止表单进行伪造)
    protected $insertFields = 'goods_name,market_price,shop_price,is_on_sale,goods_desc,brand_id';
    // 修改时调用create方法允许接收的字段
    protected $updateFields = 'id,goods_name,market_price,shop_price,is_on_sale,goods_desc,brand_id';
    //定义验证规则
    protected $_validate = array(
        //1表示必须验证0存在就验证2值不为空的时候验证
        array('goods_name', 'require', '商品名称不能为空!', 1),
        array('market_price', 'currency', '市场价格必须为货币类型!', 1),
        array('shop_price', 'currency', '本店价格必须为货币类型!', 1),
    );

    public function search($pageSize = 2)
    {
        /****************搜索*******************/
        $where = array();
        //商品名称
        if ($gn = I('get.gn'))
            $where['a.goods_name'] = array('like', "%$gn%");
        //本店价格
        $fp = I('get.fp');
        $tp = I('get.tp');
        if ($fp && $tp) {
            $where['a.shop_price'] = array('between', array($fp, $tp));
        } else if ($fp) {
            $where['a.shop_price'] = array('egt', $fp);
        } else if ($tp) {
            $where['a.shop_price'] = array('elt', $tp);
        }
        //是否上架
        $ios = I('get.ios');
        if ($ios) {
            $where['a.is_on_sale'] = array('eq', $ios);
        }
        //添加时间
        $fa = I('get.fa');
        $ta = I('get.ta');
        if ($fa && $ta) {
            $where['a.addtime'] = array('between', array($fa, $ta));
        } elseif ($fa) {
            $where['a.addtime'] = array('egt', $fa);
        } elseif ($ta) {
            $where['a.addtime'] = array('elt', $ta);
        }
        //品牌
        $brandId = I('get.brand_id');
        if ($brandId)
            $where['a.brand_id'] = array('eq', $brandId);

        /******************************* 翻页 ******************************/
        $count = $this->alias("a")->where($where)->count();//数据总数
        //调用分页类,返回分页对象
        $page = mypage($count, $pageSize);
        $data['page'] = $page->show();
        /****************排序*******************/
        $orderby = 'a.id';  //默认的排序字段
        $orderway = 'desc';//默认的排序方式
        $odby = I('get.odby');
        if ($odby) {
            if ($odby == 'id_asc')
                $orderway = 'asc';
            elseif ($odby == 'price_desc')
                $orderby = 'shop_price';
            elseif ($odby == 'price_asc') {
                $orderby = 'shop_price';
                $orderway = 'asc';
            }
        }

        /*****************************取数据*******************************/
        $data['data'] = $this->alias('a')->field('a.*,b.brand_name')->join('LEFT JOIN __BRAND__ b ON a.brand_id = b.id')->where($where)->limit($page->firstRow . ',' . $page->listRows)->order("$orderby $orderway")->select();
        return $data;
    }

    //插入前
    protected function _before_insert(&$data, $option)
    {
        $data['addtime'] = date('Y-m-d H:i:s', time());

        if (isset($_FILES['logo']) && $_FILES['logo']['error'] == 0) {
            $ret = uploadOne('logo', 'Goods', array(array(50, 50), array(130, 130,), array(350, 350), array(700, 700)));
            if ($ret['ok'] == 1) {
                $data['logo'] = $ret['images'][0];  //0是原图
                $data['sm_logo'] = $ret['images'][1];
                $data['mid_logo'] = $ret['images'][2];
                $data['big_logo'] = $ret['images'][3];
                $data['mbig_logo'] = $ret['images'][4];
            } else {
                $this->error = $ret['error'];
                return false;
            }
        }
    }

    //插入后
    protected function _after_insert($data, $option)
    {
        /*************处理相册图片*********************/
        if (isset($_FILES['pic'])) {
            $pics = array();
            foreach ($_FILES['pic']['name'] as $k => $v) {
                $pics[] = array(
                    'name' => $v,
                    'type' => $_FILES['pic']['type'][$k],
                    'tmp_name' => $_FILES['pic']['tmp_name'][$k],
                    'error' => $_FILES['pic']['error']['$k'],
                    'size' => $_FILES['pic']['size'][$k],
                );
            }
            //把处理好的数据赋值给$_FILES,因为uploadOne函数是到$_FILES中找图片
            $_FILES = $pics;
            $gpModel = D('goods_pic');
            //循环每个上传
            foreach ($pics as $k => $v) {
                if ($v['error'] == 0) {
                    $ret = uploadOne($k, 'Goods', array(
                        array(650, 650),
                        array(350, 350),
                        array(50, 50),
                    ));
                    if ($ret['ok'] == 1) {
                        //添加商品的相册
                        $gpModel->add(array(
                            'pic' => $ret['images'][0],
                            'big_pic' => $ret['images'][1],
                            'mid_pic' => $ret['images'][2],
                            'sm_pic' => $ret['images'][3],
                            'goods_id' => $data['id'],
                        ));
                    }
                }
            }
        }
        /************ 处理会员价格 ****************/
        $mp = I('post.member_price');
        $mpModel = D('member_price');
        foreach ($mp as $k => $v) {
            $_v = (float)$v;
            // 如果设置了会员价格就插入到表中
            if ($_v > 0) {
                $mpModel->add(array(
                    'price' => $_v,
                    'level_id' => $k,
                    'goods_id' => $data['id'],
                ));
            }
        }
    }

    //修改前  这时就会有id存在于option中  $data中没哟id
    protected function _before_update(&$data, $option)
    {
        $id = $option['where']['id'];
        /***************处理LOGO*****************/
        if (isset($_FILES['logo']) && $_FILES['logo']['error'] == 0) {
            $ret = uploadOne('logo', 'Goods', array(array(50, 50), array(130, 130,), array(350, 350), array(700, 700)));
            if ($ret['ok'] == 1) {
                $data['logo'] = $ret['images'][0];  //0是原图
                $data['sm_logo'] = $ret['images'][1];
                $data['mid_logo'] = $ret['images'][2];
                $data['big_logo'] = $ret['images'][3];
                $data['mbig_logo'] = $ret['images'][4];
            } else {
                $this->error = $ret['error'];
                return false;
            }
            /*************** 删除原来的图片 *******************/
            // 先查询出原来图片的路径
            $oldLogo = $this->field('logo,mbig_logo,big_logo,mid_logo,sm_logo')->find($id);
            deleteImage($oldLogo);
        }
        /**************处理相册图片*****************/
        if (isset($_FILES['pic'])) {
            $pics = array();
            foreach ($_FILES['pic']['name'] as $k => $v) {
                $pics[] = array(
                    'name' => $v,
                    'type' => $_FILES['pic']['type'][$k],
                    'tmp_name' => $_FILES['pic']['tmp_name'][$k],
                    'error' => $_FILES['pic']['error'][$k],
                    'size' => $_FILES['pic']['size'][$k],
                );
            }
            $_FILES = $pics;  // 把处理好的数组赋给$_FILES，因为uploadOne函数是到$_FILES中找图片
            $gpModel = D('goods_pic');
            // 循环每个上传
            foreach ($pics as $k => $v) {
                if ($v['error'] == 0) {
                    $ret = uploadOne($k, 'Goods', array(
                        array(650, 650),
                        array(350, 350),
                        array(50, 50),
                    ));
                    if ($ret['ok'] == 1) {
                        $gpModel->add(array(
                            'pic' => $ret['images'][0],
                            'big_pic' => $ret['images'][1],
                            'mid_pic' => $ret['images'][2],
                            'sm_pic' => $ret['images'][3],
                            'goods_id' => $id,
                        ));
                    }
                }
            }
        }
        /**************处理会员价格****************/
        $mp = I('post.member_price');
        $mpModel = D("member_price");
        //先删除原来的会员价格
        $mpModel->where(array('goods_id' => array('eq', $id)))->delete();
        foreach ($mp as $k => $v) {
            $_v = (float)$v;
            //如果设置了会员价格就插入表中,没有填写就不往表中插入了
            if ($_v > 0) {
                $mpModel->add(array(
                    'price' => $_v,
                    'level_id' => $k,
                    'goods_id' => $id,
                ));
            }
        }
    }

    protected function _before_delete($option)
    {
        //删除前将上传的图片先删除掉
        $id = $option['where']['id'];
        // 先查询出原来图片的路径
        $oldLogo = $this->field('logo,mbig_logo,big_logo,mid_logo,sm_logo')->find($id);
        deleteImage($oldLogo);
        /***********删除相册中的图片************/
        //先从相册中取出相册所在硬盘的路径
        $gpModel = D('goods_pic');
        //取出的是二维数组
        $pics = $gpModel->field('pic,sm_pic,mid_pic,big_pic')->where(
            array('goods_id' => array('eq', $id))
        )->select();
        foreach ($pics as $k => $v) {
            deleteImage($v);
        }

        //从数据库中把记录删除
        $gpModel->where(array(
            'goods_id' => array('eq', $id),
        ))->delete();
    }
}


















